import VueRouter from "vue-router"
import Vue from "vue"

Vue.use(VueRouter);

// darshboard

// let home_pages = [
//     {
//         name: "home",
//         path: "/home",
//         component: () => import("@/views/dashboard/index.vue"),
//     }
// ];

// user

// let user_pages = [
//     // {
//     //     name: "userlist",
//     //     path: "/userlist",
//     //     component: () => import("@/views/user/user_list.vue"),
//     // },

// ];


// login && layout

let login = [{
  name: "login",
  path: "/login",
  component: () => import("@/views/login/index.vue"),
},
{
  name: "reg",
  path: "/reg",
  component: () => import("@/views/login/regesit.vue"),
}
]

let shop = [
  {
    name: "shop",
    path: "/shop",
    component: () => import("@/views/shop/index.vue"),
  }, {
    name: "shoplist",
    path: "/shoplist",
    component: () => import("@/views/shop/list.vue"),
  }
]


let food = {
  name: "food",
  path: "/food",
  component: () => import("@/views/food/index.vue"),
}
let foodcate = {
  name: "foodcate",
  path: "/foodcate",
  component: () => import("@/views/shop/foodcate.vue"),
}
let shopcar = {
  name: "shopcar",
  path: "/shopcar",
  component: () => import("@/views/shopcar/index.vue"),
}

// layout
let Index = {
  name: "layout",
  path: "/index",
  component: () => import("@/views/index/index.vue"),
}

let mineedit = {
  name: "mineedit",
  path: "/mineedit",
  component: () => import("@/views/mine/edit.vue"),

}

// pay
let paypage = {
  name: "paypage",
  path: "/paypage",
  component: () => import("@/views/pay/index.vue"),
}
let payresult = {
  name: "payresult",
  path: "/payresult",
  component: () => import("@/views/payresult/index.vue"),
}


// order
let order = [{
  name: "orderconfirm",
  path: "/orderconfirm",
  component: () => import("@/views/order/order_confirm.vue"),
}, {
  name: "orderlist",
  path: "/orderlist",
  component: () => import("@/views/order/list.vue"),
}, {
  name: "order",
  path: "/order",
  component: () => import("@/views/order/index.vue"),
},
{
  name: "orderdetail",
  path: "/orderdetail",
  component: () => import("@/views/order/detail.vue"),
},
{
  name: "ordermember",
  path: "/ordermember",
  component: () => import("@/views/order/member.vue"),
}
];



let layout = {
  name: "main",
  path: "/",
  redirect: () => {
    return {
      path: '/index'
    }
  },
}

const router = new VueRouter({
  routes: [
    ...login,
    Index,
    ...shop,
    food,
    mineedit,
    shopcar,
    paypage,
    foodcate,
    payresult,
    ...order,
    layout,
  ]
})

router.beforeEach((to, from, next) => {
  if (to.name == "login") {
    next()
  } else {
    let hasLogin = localStorage.getItem("login")
    if (hasLogin == 0) {
      next("/login");
    } else {
      next();
    }
  }
})


export default router;
